#include "Main/stdafx.h"
#include "BJLPlayer.h"
#include "BJLSoundFun.h"
//#include "Games/NiuNiuCardSprite.h"
//#include "Games/NiuNiuGameLogic.h"
#include "Main/configConst.h";

BJLPlayer::BJLPlayer(int iIdex,cocos2d::Node* pSeatNode )
	:GamePlayer(NULL)
	,m_iIdex(iIdex)
	,m_userMoney(0)
	,m_pSeatNode(pSeatNode)
	//,m_kHandCardCout(0)
	, m_headUrl("")
{
	init();
}

BJLPlayer::~BJLPlayer()
{

}

void BJLPlayer::init()
{
	//m_handPanel = (Node*) m_pSeatNode->getChildByName(StringUtils::format("Panel_card%d", m_iIdex));

	//m_PushPanel = (Sprite*)m_pSeatNode->getChildByName(StringUtils::format("Sprite_m%d", m_iIdex));
	//m_PushPanel->setVisible(false);

	m_uPanel = (Layout*)m_pSeatNode->getChildByName(StringUtils::format("Panel_player%d", m_iIdex));
	m_uPanel->setVisible(false);

	m_head = (Sprite*)m_uPanel->getChildByName("Sprite_head");
	m_headFrame = (Sprite*)m_uPanel->getChildByName("Sprite_player");
	m_offLine_sprite = (Sprite*)m_uPanel->getChildByName("off_line");
	m_zhuang_sprite = (Sprite*)m_uPanel->getChildByName("Sprite_king");
	m_ready_sprite = (Sprite*)m_uPanel->getChildByName("Sprite_d");
	//m_userInfo_name = (Text*)m_uPanel->getChildByName("Text_name"); 
	m_userInfo_money = (Text*)m_uPanel->getChildByName("Text_money");

	m_addScore = (Text*)m_uPanel->getChildByName("Text_AddScore");

	PlayerLeave();
}
int  BJLPlayer::getIdex()
{
	return m_iIdex;
}
void BJLPlayer::defaultState()
{
	//m_kHandCardCout = 0;

	m_zhuang_sprite->setVisible(false);
	m_ready_sprite->setVisible(false);
	m_offLine_sprite->setVisible(false);
	//m_PushPanel->setVisible(false);
	//m_handPanel->removeAllChildrenWithCleanup(true);

	m_addScore->setVisible(false);

	upPlayerState();
}

void BJLPlayer::startGame()
{
	m_ready_sprite->setVisible(false);
}

void BJLPlayer::EndGame()
{
	if (m_pUserItem == NULL )
	{
		return;
	}
	//m_PushPanel->setVisible(false);
}

void BJLPlayer::PlayerEnter()
{
	m_uPanel->setVisible(true);
	m_offLine_sprite->setVisible(false);

	std::string updateHeadUrl = StringUtils::format("%s/AvatarInterface.aspx?type=getList&id=%d", bzyfk_url.c_str(), GetUserID());
	MCWebReq::instance().sendRequestDocumentUrl(updateHeadUrl, nullptr, CC_CALLBACK_1(BJLPlayer::Rsp_uptHeadUrl_list, this));
}

void BJLPlayer::PlayerLeave()
{
	defaultState();
	m_userMoney = 0;
	m_headUrl = "";
	//m_PushPanel->setVisible(false);
	m_uPanel->setVisible(false);
}

bool BJLPlayer::checkUserSitDown()
{
	return m_uPanel->isVisible();
}

void BJLPlayer::upPlayerInfo()
{
	//m_userInfo_name->setString(GetNickName()); 
	m_userInfo_money->setString(StringUtils::format("%lld", m_userMoney));
	/*
	if (GetHeadHttp() != "")  
	{
		ImagicDownManager::Instance().addDown(m_head,
			GetHeadHttp(),GetUserID());
	}
	else
	{
		if (GetGender() == 0)
			m_head->setTexture("common/Head0.png");
		else
			m_head->setTexture("common/Head1.png");
	}
	*/
}
void BJLPlayer::setZhuang()
{
	m_zhuang_sprite->setVisible(true);
}
void BJLPlayer::showAddGold(SCORE iGold)
{
	m_addScore->setVisible(true);
	if (iGold >= 0)
	{
		m_addScore->setTextColor(cocos2d::ccColor4B(183, 12, 1, 255));
		m_addScore->setString(StringUtils::format("+%lld", iGold));
	}
	else
	{
		m_addScore->setTextColor(cocos2d::ccColor4B(30, 144, 255, 255));
		m_addScore->setString(StringUtils::format("%lld", iGold));
	}
}

void BJLPlayer::hideAddGold()
{
	m_addScore->setVisible(false);
}

void BJLPlayer::upPlayerState()
{
	if (GetUserStatus() == US_READY)
	{
		m_ready_sprite->setVisible(true);
	}
	if (GetUserStatus() == US_OFFLINE)
	{
		m_offLine_sprite->setVisible(true);
	}
	else
	{
		m_offLine_sprite->setVisible(false);
	}
}

int BJLPlayer::getSelectedCardNum()
{
	int initNum = 0;
	//for (int i = 0; i < m_handPanel->getChildrenCount(); i++)
	//{
	//CCardSprite* pSprite = dynamic_cast<CCardSprite*>(m_handPanel->getChildren().at(i));
	//if (pSprite == NULL || pSprite->getTag() != 1)
	//{
	//continue;
	//}
	//if (pSprite->getStatus() == CCardSprite::ST_SELECTED)
	//initNum++;
	//}
	return initNum;
}

void BJLPlayer::refreshNiuPanel(Node* niuPanel)
{
	/*initNiuPanel(niuPanel);

	int initIndex = 1;
	int totalValue = 0;
	for (int i = 0; i < m_handPanel->getChildrenCount(); i++)
	{
		CCardSprite* pSprite = dynamic_cast<CCardSprite*>(m_handPanel->getChildren().at(i));
		if (pSprite == NULL || pSprite->getTag() != 1)
		{
			continue;
		}
		if (pSprite->getStatus() == CCardSprite::ST_SELECTED)
		{
			TextAtlas* textAtlas = (TextAtlas*)niuPanel->getChildByName(StringUtils::format("AtlasLabel_%d", initIndex));
			BYTE currCard = getTouchCardVlaue(pSprite);
			int cardValue = currCard & LOGIC_MASK_VALUE;
			if (cardValue >= 10)
				cardValue = 10;
			totalValue += cardValue;
			textAtlas->setString(StringUtils::format("%d", cardValue));
			initIndex++;
		}
	}
	TextAtlas* textAtlas = (TextAtlas*) niuPanel->getChildByName("AtlasLabel_4");
	textAtlas->setString(StringUtils::format("%d", totalValue));*/
}

void BJLPlayer::initNiuPanel(Node* niuPanel)
{
	for (auto taItem : niuPanel->getChildren())
	{
		TextAtlas* textAtlas = (TextAtlas*)taItem;
		textAtlas->setString("0");
	}
}

void BJLPlayer::showResultType(BYTE bOpen)
{
	//if (m_handPanel->getChildrenCount() > 2)
	//{
	//	CCardSprite* cardSprite = (CCardSprite*)m_handPanel->getChildren().at(2);
	//	Vec2 endpos = cardSprite->getPosition();
	//	//endpos.x += cardSprite->getBoundingBox().size.width / 2;

	//	endpos.x += cardSprite->getBoundingBox().size.width * 4;

	//	Sprite* niuSprite = getNiuNiuTypeResult(bOpen);
	//	m_handPanel->addChild(niuSprite, 99, 99);
	//	endpos.y += niuSprite->getBoundingBox().size.height / 2; 
	//	niuSprite->setPosition(endpos);
	//}
}

Sprite* BJLPlayer::getNiuNiuTypeResult(int type)
{
	Sprite* niuSprite = NULL;
	/*switch (type)
	{
	case OX_VALUE0:
	niuSprite = Sprite::create("game2/type_00.png");
	break;
	case OX_NIU1:
	niuSprite = Sprite::create("game2/type_01.png");
	break;
	case OX_NIU2:
	niuSprite = Sprite::create("game2/type_02.png");
	break;
	case OX_NIU3:
	niuSprite = Sprite::create("game2/type_03.png");
	break;
	case OX_NIU4:
	niuSprite = Sprite::create("game2/type_04.png");
	break;
	case OX_NIU5:
	niuSprite = Sprite::create("game2/type_05.png");
	break;
	case OX_NIU6:
	niuSprite = Sprite::create("game2/type_06.png");
	break;
	case OX_NIU7:
	niuSprite = Sprite::create("game2/type_07.png");
	break;
	case OX_NIU8:
	niuSprite = Sprite::create("game2/type_08.png");
	break;
	case OX_NIU9:
	niuSprite = Sprite::create("game2/type_09.png");
	break;
	case OX_NIUNIU:
	niuSprite = Sprite::create("game2/type_10.png");
	break;
	case OX_THREE_SAME:
	case OX_FOUR_SAME:
	case OX_FOURKING:
	niuSprite = Sprite::create("game2/type_11.png");
	break;
	case OX_FIVEKING:
	niuSprite = Sprite::create("game2/type_12.png");
	break;
	default:
	niuSprite = Sprite::create("game2/type_00.png");
	break;
	}*/
	return niuSprite;
}

void BJLPlayer::showResultScore(int64 resultScore)
{
	updateUserMoney(resultScore);

	//m_offLine_sprite

	//Sprite* typeSprite = (Sprite*)m_handPanel->getChildByTag(99);
	if (m_headFrame)
	{
		//TextAtlas* tAtlas = NULL;
		Text* txt_jieshuan = (Text*)m_pSeatNode->getChildByName("Text_jiesuan");
		SCORE retMoney = 0;
		std::string  m_fuhao = "";
		if (resultScore > 0)
		{
			//tAtlas = (TextAtlas*)m_pSeatNode->getChildByName("AtlasLabel_win");
			retMoney = resultScore;
			txt_jieshuan->setTextColor(cocos2d::ccColor4B(183, 12, 1, 255));
			m_fuhao = "+";
		}
		else
		{
			//tAtlas = (TextAtlas*)m_pSeatNode->getChildByName("AtlasLabel_lost");
			retMoney = abs(resultScore);
			txt_jieshuan->setTextColor(cocos2d::ccColor4B(30, 144, 255, 255));
			m_fuhao = "-";
		}
		
		Text* newText = (Text*)txt_jieshuan->clone();
		newText->setString(StringUtils::format("%s%lld", m_fuhao.c_str(),retMoney));
		newText->setScale(2.5f);
		m_headFrame->addChild(newText, 100, 100);
		//newText->setPosition(typeSprite->getPosition());
		newText->setPosition(Vec2(m_headFrame->getContentSize().width / 2, m_headFrame->getPosition().y));
		Vec2 endPos = Vec2(0, m_headFrame->getContentSize().height / 2 + newText->getContentSize().height / 2);
		if (m_iIdex == 0)
			endPos.y += m_headFrame->getContentSize().height / 4;
		newText->runAction(Spawn::createWithTwoActions(MoveBy::create(1.0f, endPos), ScaleTo::create(1.0f, 1.0f)));
	}
}

void BJLPlayer::showChatWord(std::string szChatString)
{
	Vec2 endPos = m_uPanel->getPosition();
	Layout* chatPanel = NULL;
	if (m_iIdex > 6)
	{
		chatPanel = (Layout*)m_pSeatNode->getChildByName("Image_chatRight");
		endPos.x -= m_uPanel->getContentSize().width;
		endPos.x -= chatPanel->getContentSize().width / 2;
	}
	else if (m_iIdex > 3)
	{
		chatPanel = (Layout*)m_pSeatNode->getChildByName("Image_chatLeft");
		endPos.y -= m_uPanel->getContentSize().height;
		endPos.y -= chatPanel->getContentSize().height / 2;
	}
	else if (m_iIdex == 0)
	{
		chatPanel = (Layout*)m_pSeatNode->getChildByName("Image_chatLeft");
		endPos.x += m_uPanel->getContentSize().width;
		endPos.x += chatPanel->getContentSize().width / 2;
		endPos.y += m_uPanel->getContentSize().height;
		endPos.y += chatPanel->getContentSize().height / 2;
	}
	else
	{
		chatPanel = (Layout*)m_pSeatNode->getChildByName("Image_chatLeft");
		endPos.x += m_uPanel->getContentSize().width;
		endPos.x += chatPanel->getContentSize().width / 2;
	}
	Layout* newPanel = (Layout*)chatPanel->clone();
	Text* chatText = (Text*)newPanel->getChildByName("Text_value");
	chatText->setString(szChatString);
	m_pSeatNode->addChild(newPanel);
	newPanel->setPosition(endPos);
	newPanel->runAction(Sequence::create(DelayTime::create(2.0f), RemoveSelf::create(), NULL));
}

void BJLPlayer::showChatFace(word wItemIndex)
{
	Vec2 endPos = m_uPanel->getPosition();
	Sprite* chatPanel = Sprite::create(StringUtils::format("chat/face%d.png", wItemIndex + 1));
	if (m_iIdex > 6)
	{
		endPos.x -= m_uPanel->getContentSize().width;
		endPos.x -= chatPanel->getContentSize().width / 2;
	}
	else if (m_iIdex > 3)
	{
		endPos.y -= m_uPanel->getContentSize().height;
		endPos.y -= chatPanel->getContentSize().height / 2;
	}
	else if (m_iIdex == 0)
	{
		endPos.x += m_uPanel->getContentSize().width;
		endPos.x += chatPanel->getContentSize().width / 2;
		endPos.y += m_uPanel->getContentSize().height;
		endPos.y += chatPanel->getContentSize().height / 2;
	}
	else
	{
		endPos.x += m_uPanel->getContentSize().width;
		endPos.x += chatPanel->getContentSize().width / 2;
	}
	m_pSeatNode->addChild(chatPanel);
	chatPanel->setPosition(endPos);
	chatPanel->runAction(Sequence::create(DelayTime::create(2.0f), RemoveSelf::create(), NULL));
}

void BJLPlayer::initUserMoney(SCORE money)
{
	m_userMoney = money;
	m_userInfo_money->setString(StringUtils::format("%lld", m_userMoney));
}

void BJLPlayer::updateUserMoney(SCORE money)
{
	m_userMoney += money;
	m_userInfo_money->setString(StringUtils::format("%lld", m_userMoney));
}

void BJLPlayer::Rsp_uptHeadUrl_list(const std::string& data)
{
	rapidjson::Document doc;
	doc.Parse<rapidjson::kParseDefaultFlags>(data.c_str());
	if (doc.HasParseError() || !doc.HasMember("Avatar"))
	{
		return;
	}
	std::string strUrl = doc["Avatar"].GetString();
	if (strUrl.find("http") != std::string::npos)
	{
		m_headUrl = strUrl;
		ImagicDownManager::Instance().addDown(m_head,
			strUrl, GetUserID());
	}
	else
	{
		//if (GetGender() == 0)
		//	m_head->setTexture("common/Head0.png");
		//else
		//	m_head->setTexture("common/Head1.png");
	}
}

void BJLPlayer::showDefaultSound(int itemIndex)
{
	BJLSoundFun::playEffect(StringUtils::format("default/default%d", itemIndex));
}

